<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="vue 中使用 Element 的 upload 组件上传 Excel，大致可以分两种情况  使用 action 上传到服务器 使用 axios 上传到服务器">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue 中使用 Upload 组件上传 Excel">
<meta property="og:url" content="http://llmysnow.top/posts/5c61fd6a/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="vue 中使用 Element 的 upload 组件上传 Excel，大致可以分两种情况  使用 action 上传到服务器 使用 axios 上传到服务器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/upload_options.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/%E4%B8%8A%E4%BC%A0xlsx.gif">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/%E5%AF%BC%E5%85%A5Excel.gif">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/upload_file_raw.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/upload_xlsx.jpg">
<meta property="article:published_time" content="2021-07-02T09:28:37.958Z">
<meta property="article:modified_time" content="2021-07-06T01:48:09.985Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="Element">
<meta property="article:tag" content="封装">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/upload_options.jpg">

<link rel="canonical" href="http://llmysnow.top/posts/5c61fd6a/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Vue 中使用 Upload 组件上传 Excel | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/5c61fd6a/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue 中使用 Upload 组件上传 Excel
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-02 17:28:37" itemprop="dateCreated datePublished" datetime="2021-07-02T17:28:37+08:00">2021-07-02</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>vue 中使用 Element 的 upload 组件上传 Excel，大致可以分两种情况</p>
<ol>
<li>使用 <code>action</code> 上传到服务器</li>
<li>使用 <code>axios</code> 上传到服务器</li>
</ol>
<a id="more"></a>

<p><strong>注意：上传文件可能由于前后端格式不统一导致上传失败</strong></p>
<ul>
<li><code>application/x-www-form-urlencoded</code> 一般情况下使用这个比较多</li>
<li><code>multipart/form-data</code></li>
<li><code>application/json</code></li>
</ul>
<h2 id="使用-action"><a href="#使用-action" class="headerlink" title="使用 action"></a>使用 <code>action</code></h2><p>使用 <code>action</code> 时，首先会使用OPTIONS方法发起一个<strong>预检请求</strong>，从而获知服务器是否允许该跨域请求。服务器确认允许之后，才发起实际的 HTTP 请求。后端返回 204，以防处理 POST 请求时访问错误</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/upload_options.jpg"></p>
<p><strong>注意：使用 <code>action</code> ，需要后端做跨域处理。比如 Nginx <a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903782556368910">反向代理</a>、CORS 等</strong></p>
<p>效果如下：</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/js-img/%E4%B8%8A%E4%BC%A0xlsx.gif"></p>
<p>备注：</p>
<ol>
<li>如果希望使用 ajax 发送请求可以配置 <code>http-request</code></li>
<li>Window 电脑可以选择 <code>所有文件（*.*）</code> ，之后可以上传任意文件，最好在上传之前做个 <code>before-upload</code> 判断类型处理</li>
</ol>
<p>代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    &lt;el-upload</span><br><span class="line">      ref=&quot;upload&quot;</span><br><span class="line">      :accept=&quot;fileType.join(&#x27;,&#x27;)&quot;</span><br><span class="line">      :limit=&quot;1&quot;</span><br><span class="line">      :headers=&quot;upload.headers&quot;</span><br><span class="line">      :action=&quot;upload.url&quot;</span><br><span class="line">      :disabled=&quot;upload.isUploading&quot;</span><br><span class="line">      :before-upload=&quot;beforeUpload&quot;</span><br><span class="line">      :on-progress=&quot;handleFileProgress&quot;</span><br><span class="line">      :on-success=&quot;handleFileSuccess&quot;</span><br><span class="line">      :auto-upload=&quot;false&quot;</span><br><span class="line">      drag</span><br><span class="line">    &gt;</span><br><span class="line">      <span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;el-icon-upload&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;el-upload__text&quot;</span>&gt;</span>将文件拖到此处，或<span class="tag">&lt;<span class="name">em</span>&gt;</span>点击上传<span class="tag">&lt;/<span class="name">em</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">slot</span>=<span class="string">&quot;tip&quot;</span> <span class="attr">class</span>=<span class="string">&quot;el-upload__tip&quot;</span> <span class="attr">style</span>=<span class="string">&quot;color: red&quot;</span>&gt;</span></span><br><span class="line">        提示：仅允许导入“xls”或“xlsx”格式文件！</span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">el-upload</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">&quot;success&quot;</span> <span class="attr">size</span>=<span class="string">&quot;mini&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;submitUpload&quot;</span>&gt;</span>上传到服务器<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; getToken &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/auth&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;Upload&#x27;</span>,</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">      fileType: [<span class="string">&#x27;.xlsx&#x27;</span>, <span class="string">&#x27;.xls&#x27;</span>],</span></span><br><span class="line">      upload: &#123;</span><br><span class="line"><span class="javascript">        <span class="comment">// 设置上传的请求头部</span></span></span><br><span class="line">        headers: &#123; Authorization: getToken() &#125;,</span><br><span class="line"><span class="javascript">        <span class="comment">// 上传地址</span></span></span><br><span class="line"><span class="javascript">        url: <span class="string">&#x27;https://jsonplaceholder.typicode.com/posts/&#x27;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 是否更新已经存在的用户数据</span></span></span><br><span class="line"><span class="javascript">        isUploading: <span class="literal">false</span></span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    <span class="comment">// 文件上传中处理</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">handleFileProgress</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.upload.isUploading = <span class="literal">true</span></span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 文件上传成功处理</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">handleFileSuccess</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.upload.isUploading = <span class="literal">false</span></span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$refs.upload.clearFiles()</span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 提交上传文件</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">submitUpload</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$refs.upload.submit()</span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 上传文件之前的钩子</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">beforeUpload</span>(<span class="params">file</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> isXlsx = file.type === <span class="string">&#x27;application/vnd.ms-excel&#x27;</span></span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (!isXlsx) &#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.$message.error(<span class="string">&#x27;上传文件只能是 xlsx 或 xls 格式&#x27;</span>)</span></span><br><span class="line">      &#125;</span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> isXlsx</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="不使用-action"><a href="#不使用-action" class="headerlink" title="不使用 action"></a>不使用 <code>action</code></h2><p>上面需要后端配合使用，沟通起来还是比较麻烦的，还是推荐不使用 <code>action</code>，自己处理 ajax 请求可以更自由些</p>
<p>效果如下（数据是拿 mock 随机生成存入 Excel 的）：</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/%E5%AF%BC%E5%85%A5Excel.gif"></p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader">FileReader - MDN</a></p>
<ul>
<li>想把文件以断点续传的形式传给服务器，一般使用 <code>readAsArrayBuffer()</code> 读取文件</li>
<li>想把文件中的数据展示到页面上，一般使用 <code>readAsBinaryString()</code> 读取文件</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN/component/upload">Element Upload</a></p>
<ul>
<li><p><code>:on-change</code> 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用</p>
<p>第一个参数是 file，里面有文件内容、状态等信息</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/upload_file_raw.jpg"></p>
</li>
<li><p>把文件内容通过 FileReader 转换成二进制文件后</p>
<p>再通过 <code>xlsx.read</code> 读取，即可拿到 Excel 数据内容</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/upload_xlsx.jpg"></p>
</li>
<li><p>最后通过 <code>xlsx.utils.sheet_to_json</code> 即可转换成我们需要的数据格式</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN/component/loading">Element Loading</a></p>
<ul>
<li><p><code>Loading.service(options)</code> 以服务的方式调用的 Loading 需要异步关闭</p>
<p>需结合 <code>this.$nextTick()</code> 使用</p>
</li>
</ul>
<p>代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;uploadBox&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 上传文件按钮 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;buttonBox&quot;</span>&gt;</span></span><br><span class="line">      &lt;el-upload</span><br><span class="line">        action</span><br><span class="line">        accept=&quot;.xlsx,.xls&quot;</span><br><span class="line">        :show-file-list=&quot;false&quot;</span><br><span class="line">        :on-change=&quot;handleChange&quot;</span><br><span class="line">        :auto-upload=&quot;false&quot;</span><br><span class="line">      &gt;</span><br><span class="line">        <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">slot</span>=<span class="string">&quot;trigger&quot;</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span>&gt;</span>选取Excel文件<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">&quot;success&quot;</span> <span class="attr">:disabled</span>=<span class="string">&quot;disabled&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;submit&quot;</span>&gt;</span>提交到服务器<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-upload</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 解析出来的数据 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-show</span>=<span class="string">&quot;show&quot;</span> <span class="attr">class</span>=<span class="string">&quot;tableBox&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;el-icon-info&quot;</span>&gt;</span>请您检查无误后，再点击“提交到服务器”按钮<span class="tag">&lt;/<span class="name">i</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-table</span> <span class="attr">:data</span>=<span class="string">&quot;tempData&quot;</span> <span class="attr">border</span> <span class="attr">style</span>=<span class="string">&quot;width: 100%&quot;</span> <span class="attr">:height</span>=<span class="string">&quot;height&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-table-column</span> <span class="attr">prop</span>=<span class="string">&quot;name&quot;</span> <span class="attr">label</span>=<span class="string">&quot;姓名&quot;</span> <span class="attr">min-width</span>=<span class="string">&quot;50%&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-table-column</span> <span class="attr">prop</span>=<span class="string">&quot;phone&quot;</span> <span class="attr">label</span>=<span class="string">&quot;电话&quot;</span> <span class="attr">min-width</span>=<span class="string">&quot;50%&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-table</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> xlsx <span class="keyword">from</span> <span class="string">&#x27;xlsx&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Loading &#125; <span class="keyword">from</span> <span class="string">&#x27;element-ui&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> uploadExcel <span class="keyword">from</span> <span class="string">&#x27;@/api&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;Upload&#x27;</span>,</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">      height: <span class="built_in">document</span>.documentElement.clientHeight - <span class="number">130</span>,</span></span><br><span class="line">      tempData: [],</span><br><span class="line"><span class="javascript">      show: <span class="literal">false</span>,</span></span><br><span class="line"><span class="javascript">      disabled: <span class="literal">false</span>,</span></span><br><span class="line">      character: &#123;</span><br><span class="line">        name: &#123;</span><br><span class="line"><span class="javascript">          text: <span class="string">&#x27;姓名&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          type: <span class="string">&#x27;string&#x27;</span></span></span><br><span class="line">        &#125;,</span><br><span class="line">        phone: &#123;</span><br><span class="line"><span class="javascript">          text: <span class="string">&#x27;电话&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          type: <span class="string">&#x27;string&#x27;</span></span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    <span class="comment">// 采集excel数据</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">async</span> <span class="function"><span class="title">handleChange</span>(<span class="params">file</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> originData = file.raw</span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (!originData) <span class="keyword">return</span></span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.show = <span class="literal">false</span></span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> loadingInstance = Loading.service(&#123;</span></span><br><span class="line"><span class="javascript">        text: <span class="string">&#x27;努力加载中!!!&#x27;</span>,</span></span><br><span class="line"><span class="javascript">        background: <span class="string">&#x27;rgba(0, 0, 0, 0.8)&#x27;</span></span></span><br><span class="line">      &#125;)</span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> binaryData = <span class="keyword">await</span> <span class="built_in">this</span>.readFile(originData)</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> workbook = xlsx.read(binaryData, &#123; <span class="attr">type</span>: <span class="string">&#x27;binary&#x27;</span> &#125;)</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> worksheet = workbook.Sheets[workbook.SheetNames[<span class="number">0</span>]]</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> data = xlsx.utils.sheet_to_json(worksheet)</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.tempData = <span class="built_in">this</span>.handleData(data)</span></span><br><span class="line"><span class="javascript">      <span class="keyword">await</span> <span class="built_in">this</span>.delay(<span class="number">300</span>)</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.show = <span class="literal">true</span></span></span><br><span class="line">      loadingInstance.close()</span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 把读取出来的数据转换为服务器需要的格式</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">handleData</span>(<span class="params">data</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> arr = []</span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> char = <span class="built_in">this</span>.character</span></span><br><span class="line"><span class="javascript">      data.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">const</span> obj = &#123;&#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> char) &#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">if</span> (<span class="built_in">Object</span>.hasOwnProperty.call(char, key)) &#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> el = char[key]</span></span><br><span class="line"><span class="javascript">            <span class="keyword">let</span> val = item[el.text] || <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> type = el.type</span></span><br><span class="line"><span class="javascript">            type === <span class="string">&#x27;string&#x27;</span> ? (val = <span class="built_in">String</span>(val)) : <span class="literal">null</span></span></span><br><span class="line"><span class="javascript">            type === <span class="string">&#x27;number&#x27;</span> ? (val = <span class="built_in">Number</span>(val)) : <span class="literal">null</span></span></span><br><span class="line">            obj[key] = val</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        arr.push(obj)</span><br><span class="line">      &#125;)</span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> arr</span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 提交数据给服务器</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">async</span> <span class="function"><span class="title">submit</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (<span class="built_in">this</span>.tempData.length &lt;= <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.$message(&#123;</span></span><br><span class="line"><span class="javascript">          message: <span class="string">&#x27;请先选择Excel文件&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          type: <span class="string">&#x27;warning&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          showClose: <span class="literal">true</span></span></span><br><span class="line">        &#125;)</span><br><span class="line"><span class="javascript">        <span class="keyword">return</span></span></span><br><span class="line">      &#125;</span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.disabled = <span class="literal">true</span></span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> loadingInstance = Loading.service(&#123;</span></span><br><span class="line"><span class="javascript">        text: <span class="string">&#x27;努力加载中!!!&#x27;</span>,</span></span><br><span class="line"><span class="javascript">        background: <span class="string">&#x27;rgba(0, 0, 0, 0.8)&#x27;</span></span></span><br><span class="line">      &#125;)</span><br><span class="line"><span class="javascript">      <span class="keyword">await</span> <span class="built_in">this</span>.delay(<span class="number">300</span>)</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 发送API请求</span></span></span><br><span class="line"><span class="javascript">      uploadExcel(<span class="built_in">this</span>.tempData).then(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.$message(&#123;</span></span><br><span class="line"><span class="javascript">          message: <span class="string">&#x27;Excel文件已上传完毕&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          type: <span class="string">&#x27;success&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          showClose: <span class="literal">true</span></span></span><br><span class="line">        &#125;)</span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.show = <span class="literal">false</span></span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.disabled = <span class="literal">false</span></span></span><br><span class="line">        loadingInstance.close()</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">readFile</span>(<span class="params">file</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">const</span> reader = <span class="keyword">new</span> FileReader()</span></span><br><span class="line">        reader.readAsBinaryString(file)</span><br><span class="line"><span class="javascript">        reader.onload = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span></span><br><span class="line">          resolve(e.target.result)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">delay</span>(<span class="params">interval = <span class="number">0</span></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">const</span> timer = <span class="built_in">setTimeout</span>(<span class="function"><span class="params">_</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">          <span class="built_in">clearTimeout</span>(timer)</span></span><br><span class="line">          resolve()</span><br><span class="line">        &#125;, interval)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span>&gt;</span></span><br><span class="line"><span class="css"><span class="selector-class">.buttonBox</span> &#123;</span></span><br><span class="line">  padding: 15px;</span><br><span class="line">  display: flex;</span><br><span class="line">&#125;</span><br><span class="line"><span class="css"><span class="selector-class">.el-button</span> &#123;</span></span><br><span class="line">  margin-right: 20px !important;</span><br><span class="line">&#125;</span><br><span class="line"><span class="css"><span class="selector-class">.tableBox</span> &#123;</span></span><br><span class="line">  padding: 0 15px;</span><br><span class="line">&#125;</span><br><span class="line">h3 &#123;</span><br><span class="line">  font-size: 18px;</span><br><span class="line"><span class="css">  <span class="selector-tag">color</span>: <span class="selector-id">#f56c6c</span>;</span></span><br><span class="line">  padding-bottom: 15px;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>导出数据</strong></p>
<p>如下方法使用 xlsx 导出，也可以使用 xlsx + file-saver 导出</p>
<ul>
<li><p><code>@selection-change</code> ，当选择项发生变化时会触发该事件</p>
<p>参数为 selection，拿到后处理一下</p>
</li>
<li><p>之后通过 <code>xlsx.utils.json_to_sheet</code> 将其变成 sheet</p>
<p>再新建一个表格 <code>xlsx.utils.book_new</code></p>
<p>往表格插入数据 <code>xlsx.utils.book_append_sheet</code></p>
<p>最后通过 <code>xlsx.writeFile</code> 即可下载</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = <span class="built_in">this</span>.selectionList.map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    编号: item.id,</span><br><span class="line">    姓名: item.name,</span><br><span class="line">    电话: item.phone</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">const</span> sheet = xlsx.utils.json_to_sheet(arr)</span><br><span class="line"><span class="keyword">const</span> book = xlsx.utils.book_new()</span><br><span class="line">xlsx.utils.book_append_sheet(book, sheet, <span class="string">&#x27;表格名&#x27;</span>)</span><br><span class="line">xlsx.writeFile(book, <span class="string">`user<span class="subst">$&#123;<span class="keyword">new</span> <span class="built_in">Date</span>().getTime()&#125;</span>.xls`</span>)</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/5c61fd6a/">Vue 中使用 Upload 组件上传 Excel</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/5c61fd6a/" title="Vue 中使用 Upload 组件上传 Excel">http://llmysnow.top/posts/5c61fd6a/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/5c61fd6a/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
              <a href="/tags/Element/" rel="tag"><i class="fa fa-tag"></i> Element</a>
              <a href="/tags/%E5%B0%81%E8%A3%85/" rel="tag"><i class="fa fa-tag"></i> 封装</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/e082dc34/" rel="prev" title="Element tree树组件 鼠标双击事件">
      <i class="fa fa-chevron-left"></i> Element tree树组件 鼠标双击事件
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/8188972/" rel="next" title="常见 Web 攻击（前端篇）">
      常见 Web 攻击（前端篇） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-action"><span class="nav-text">使用 action</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%8D%E4%BD%BF%E7%94%A8-action"><span class="nav-text">不使用 action</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>